package de.hibernate.example;

import javax.persistence.AssociationOverride;
import javax.persistence.AssociationOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name = "employee_course")
@AssociationOverrides({
		@AssociationOverride(name = "pk.employee", joinColumns = @JoinColumn(name = "employee_id")),
		@AssociationOverride(name = "pk.course", joinColumns = @JoinColumn(name = "course_id")) })
public class EmployeeCourse {

	private EmployeeCourseId pk = new EmployeeCourseId();
	private int degree;

	@EmbeddedId
	public EmployeeCourseId getPk() {
		return pk;
	}

	public void setPk(EmployeeCourseId pk) {
		this.pk = pk;
	}

	@Transient
	public Employee getEmployee() {
		return getPk().getEmployee();
	}

	public void setEmployee(Employee employee) {
		getPk().setEmployee(employee);
	}

	@Transient
	public Course getCourse() {
		return getPk().getCourse();
	}

	public void setCourse(Course course) {
		getPk().setCourse(course);
	}
	
	@Column(nullable=false)
	public int getDegree() {
		return degree;
	}

	public void setDegree(int degree) {
		this.degree = degree;
	}

	public boolean equals(Object o) {
		if (this == o)
			return true;
		if (o == null || getClass() != o.getClass())
			return false;

		EmployeeCourse that = (EmployeeCourse) o;

		if (getPk() != null ? !getPk().equals(that.getPk())
				: that.getPk() != null)
			return false;

		return true;
	}

	public int hashCode() {
		return (getPk() != null ? getPk().hashCode() : 0);
	}

}
